package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import modelo.Nota;
import modelo.Produto;

import conexao.ConnectionManager;

/**
 * @author Carlos, Derek e Hellen
 */
class NotaDAOImp implements NotaDAO {

	private final String INSERT = "insert into nota (dataNota, total, idCliente, idProduto) values (?,?,?,?)";

	private ConnectionManager connectionManager;
	private static Connection connection;

	NotaDAOImp() {
		this.openConnection();
	}

	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	public void openConnection() {
		if (connectionManager == null) {
			connectionManager = ConnectionManager.getInstance();
		}

		if (connection == null) {
			connection = connectionManager.getConnection();
		}

		if (isConnectionClose()) {
			connection = connectionManager.getConnection();
		}
	}

	public void closeConnection() {
		if (connection != null && connectionManager != null) {
			if (!this.isConnectionClose()) {
				connectionManager.closeConnection(connection);
			}
		}
	}

	public Nota inserirNota(Nota n) {
		try {

			PreparedStatement ps = null;

			ps = connection.prepareStatement(INSERT);

			ps.setObject(1, n.getDataNota());
			ps.setObject(2, n.getTotal());
			ps.setObject(3, n.getIdCliente());
			ps.setObject(4, n.getIdProduto());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(NotaDAOImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		return n;
	}

	@Override
	public NotaDAO salvar(NotaDAO e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void alterar(NotaDAO e) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<NotaDAO> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deletar(NotaDAO e) {
		// TODO Auto-generated method stub

	}

}
